/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2020年9月16日
 */
package com.massyframework.beanskin.web.servlet;

import java.util.Enumeration;
import java.util.Objects;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;

import com.massyframework.beanskin.Module;
import com.massyframework.beanskin.service.ServiceRepository;

/**
 * 提供关联{@link ServiceExportableModule}的ServletConfig
 * 
 * @author huangkh
 *
 */
final class ModuleServletConfig implements ServletConfig {

	private final Module<?> module;
    private final ServletConfig config;
    private final ServletContext servletContext;

    /**
     * 构造方法
     * @param module {@link ServiceExportableModule},模块
     * @param servletConfig {@link ServletConfig}, Servlet配置
     */
    public ModuleServletConfig(Module<?> module, ServletConfig servletConfig){
        this.module = Objects.requireNonNull(module, "\"module\" cannot be null.");
        this.config = Objects.requireNonNull(servletConfig, "\"servletConfig\" cannot be null.");
        this.servletContext = this.createModularServletContext();
    }

    /**
     * Returns the name of this servlet instance.
     * The name may be provided via server administration, assigned in the
     * web application deployment descriptor, or for an unregistered (and thus
     * unnamed) servlet instance it will be the servlet's class name.
     *
     * @return the name of the servlet instance
     */
    @Override
    public String getServletName() {
        return this.config.getServletName();
    }

    /**
     * Returns a reference to the {@link ServletContext} in which the caller
     * is executing.
     *
     * @return a {@link ServletContext} object, used
     * by the caller to interact with its servlet container
     * @see ServletContext
     */
    @Override
    public ServletContext getServletContext() {
        return this.servletContext;
    }

    /**
     * Gets the value of the initialization parameter with the given name.
     *
     * @param name the name of the initialization parameter whose value to
     *             get
     * @return a <code>String</code> containing the value
     * of the initialization parameter, or <code>null</code> if
     * the initialization parameter does not exist
     */
    @Override
    public String getInitParameter(String name) {
        return this.config.getInitParameter(name);
    }

    /**
     * Returns the names of the servlet's initialization parameters
     * as an <code>Enumeration</code> of <code>String</code> objects,
     * or an empty <code>Enumeration</code> if the servlet has
     * no initialization parameters.
     *
     * @return an <code>Enumeration</code> of <code>String</code>
     * objects containing the names of the servlet's
     * initialization parameters
     */
    @Override
    public Enumeration<String> getInitParameterNames() {
        return this.config.getInitParameterNames();
    }

    /**
     * 关联的模块
     * @return {@link Module}
     */
    public final Module<?> getAssociatedModule() {
        return this.module;
    }

    /**
     * 关联装配件的Servlet上下文
     * @return {@link ServletContext}
     */
    public final ServletContext getAssemblyServletContext() {
        return this.servletContext;
    }

    /**
     * 创建{@link ModularServletContext}
     * @return {@link ServletContext}
     */
    protected ServletContext createModularServletContext() {
        ServiceRepository repo = ServiceRepository.retrieveFrom(
                this.getAssociatedModule());
        return repo.getService(ServletContext.class);
    }

}
